package cn.taobaoke.restful.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

/**
 * AdminRepository类.
 *
 * @author yangzuo
 * @since 2017-03-11
 */
public interface AdminRepository extends JpaRepository<AdminEntity, Long> {

    @Query("from AdminEntity admin where (admin.isDelete = null or admin.isDelete != true) and admin.domainId = :domainId")
    List<AdminEntity> findAllAdmin(@Param("domainId") Long domainId);

    @Query("from AdminEntity admin where (admin.isDelete = null or admin.isDelete != true) and admin.id = :id and admin.domainId = :domainId")
    Optional<AdminEntity> findById(@Param("id") Long adminId, @Param("domainId") Long domainId);

    @Query("from AdminEntity admin where (admin.isDelete = null or admin.isDelete != true) and ( admin.name = :name or admin.weChat = :name ) and admin.password = :password and admin.domainId = :domainId")
    Optional<AdminEntity> findByKeywordAndPassword(@Param("name") String name, @Param("password") String password, @Param("domainId") Long domainId);

    @Query("from AdminEntity admin where (admin.isDelete = null or admin.isDelete != true) and (admin.weChat = :weChat OR admin.name = :name) and admin.domainId = :domainId")
    Optional<AdminEntity> findByWeChatOrName(@Param("weChat") String weChat, @Param("name") String name, @Param("domainId") Long domainId);

    @Query("from AdminEntity admin where (admin.isDelete = null or admin.isDelete != true) and admin.token = :token and admin.domainId = :domainId")
    Optional<AdminEntity> findByToken(@Param("token") String token, @Param("domainId") Long domainId);
}
